﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using SiteServer.Plugin;
using Thingpower.SS.EduOnline.Model;

namespace Thingpower.SS.EduOnline.Provider
{
    public class UserStudyCoursewaresDAO : BaseDAO
    {
        public const string TableName = "view_UserStudyCoursewares";


        #region 获取全部学员浏览量
        public static List<UserStudyCoursewaresInfo> GetAllModelsList()
        {
            var list = new List<UserStudyCoursewaresInfo>();

            string sqlString =
                $"SELECT UserId, CoursewareId, CoursewareTitle, Times, ChannelId, SiteId, ImageUrl FROM {TableName} ORDER BY {nameof(UserStudyCoursewaresInfo.CoursewareId)}";

            using (var rdr = DataApi.ExecuteReader(ConnectionString, sqlString))
            {
                while (rdr.Read())
                {
                    list.Add(GetModel(rdr));
                }
                rdr.Close();
            }

            return list;
        }
        #endregion

        public static List<UserStudyCoursewaresInfo> GetModelsList(int userId)
        {
            var list = new List<UserStudyCoursewaresInfo>();

            string sqlString =
                $"SELECT UserId, CoursewareId, CoursewareTitle, Times, ChannelId, SiteId, ImageUrl FROM {TableName} WHERE UserId = {userId} ORDER BY {nameof(UserStudyCoursewaresInfo.CoursewareId)}";

            using (var rdr = DataApi.ExecuteReader(ConnectionString, sqlString))
            {
                while (rdr.Read())
                {
                    list.Add(GetModel(rdr));
                }
                rdr.Close();
            }

            return list;
        }

        private static UserStudyCoursewaresInfo GetModel(IDataRecord rdr)
        {
            if (rdr == null) return null;

            var _model = new UserStudyCoursewaresInfo();

            var i = 0;
            _model.UserId = rdr.IsDBNull(i) ? 0 : rdr.GetInt32(i);
            i++;
            _model.CoursewareId = rdr.IsDBNull(i) ? 0 : rdr.GetInt32(i);
            i++;
            _model.CoursewareTitle = rdr.IsDBNull(i) ? string.Empty : rdr.GetString(i);
            i++;
            _model.Times = rdr.IsDBNull(i) ? 0 : rdr.GetInt32(i);
            i++;
            _model.ChannelId = rdr.IsDBNull(i) ? 0 : rdr.GetInt32(i);
            i++;
            _model.SiteId = rdr.IsDBNull(i) ? 0 : rdr.GetInt32(i);
            i++;
            _model.ImageUrl = rdr.IsDBNull(i) ? string.Empty : rdr.GetString(i);

            return _model;
        }
    }
}